/**
 * 日期过滤器
 */
import Vue from 'vue'

import format from 'date-fns/format'

// 自定义过滤器
Vue.filter('dateFormat', function (value, formatStr='yyyy-MM-dd') {
    // return moment(value).format(formatStr)
    if(!value) return ''
    return format(value, formatStr)
})

Vue.filter('dateFormatAll', function (value, formatStr='yyyy-MM-dd HH:mm:ss') {
    // return moment(value).format(formatStr)
    if(!value) return ''
    return format(value, formatStr)
})

Vue.filter('dateDetailFormat',function (value) {
    if(!value) return ''
    const currentDate = new Date()
    let ret = ''
    let sec = 1000
    let minute = sec * 60
    let hour = minute * 60
    let day = hour * 24
    let month = day * 30
    let year = month * 12
    let diff = currentDate - value
    let yearCount = Math.floor(diff / year)
    let monthCount = Math.floor((diff % year) / month)
    let d = Math.floor((diff % month) / day )
    let h = Math.floor( (diff % day) / hour )
    let m = Math.floor( (diff % hour) / minute )
    let s = Math.floor( (diff % minute) / sec )

    if(monthCount > 12){
        ret = `${yearCount}年前`
    }else if(monthCount > 0 && monthCount < 12) {
        ret = `${monthCount}月前`
    }else if(d < 30 && d > 1) {
        ret = `${d}天前`
    }else if(d === 1) {
        ret = `昨天`
    }else if(h < 24 && h > 1) {
        ret = `${h}小时前`
    }else if(m < 60 && m > 1) {
        ret = `${m}分钟前`
    }else{
        ret = `刚刚`
    }

    return ret
})
